#include "multiGame.h"

MultiGame::MultiGame(QWidget *parent)
    : Board{parent}
{
    redRetrackButton.reset(new QPushButton("红悔棋", this));
    redRetrackButton->setGeometry(
        BOARD_LEFT_OFFSET,
        BOARD_TOP_OFFSET + BOARD_ROW_NUM * BOARD_RECT_DIAMETER, 100, 30);
    // 连接按钮的clicked信号到成员函数
    connect(redRetrackButton.get(), &QPushButton::clicked, this, &MultiGame::onRedRetrackButtonClicked);

    blackRetrackButton.reset(new QPushButton("黑悔棋", this));
    blackRetrackButton->setGeometry(
        BOARD_LEFT_OFFSET + (BOARD_COL_NUM - 3) * BOARD_RECT_DIAMETER,
        BOARD_TOP_OFFSET + BOARD_ROW_NUM * BOARD_RECT_DIAMETER, 100, 30);
    // 连接按钮的clicked信号到成员函数
    connect(blackRetrackButton.get(), &QPushButton::clicked, this, &MultiGame::onBlackRetrackButtonClicked);
    // 连接子界面的游戏结束信号
    connect(this, &Board::gameOver, this, &MultiGame::buttonDisable);
}

void MultiGame::buttonDisable() {
    blackRetrackButton->setEnabled(false);
    redRetrackButton->setEnabled(false);
}

// 红色方悔棋
void MultiGame::onRedRetrackButtonClicked() {
    if (redSteps.empty()) {
        return;
    }
    if (isRedTurn) {
        if (blackSteps.empty()) {
            return;
        }
        // 为false指回退黑棋一步
        executeRetrack(false);
    }
    executeRetrack(true);
    // 切换走棋方
    isRedTurn = !isRedTurn;
    update();
}
// 黑色方悔棋
void MultiGame::onBlackRetrackButtonClicked() {
    if (blackSteps.empty()) {
        return;
    }
    if (!isRedTurn) {
        if (redSteps.empty()) {
            return;
        }
        // 为true指回退红棋一步
        executeRetrack(true);
    }
    executeRetrack(false);
    // 切换走棋方
    isRedTurn = !isRedTurn;
    update();
}
